//
//  FHComposeToolBar.swift
//  WeiBo
//
//  Created by qfh on 16/7/18.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit


enum FHComposeToolBarButtonType: Int {

    //  图片
    case Picture = 0
    //  @
    case Mention = 1
    //  话题
    case Trend = 2
    //  表情
    case Emoticon = 3
    //  加号
    case Add = 4

}

class FHComposeToolBar: UIStackView {

    var composeToolBarCellBack: ((buttontype: FHComposeToolBarButtonType)->())?
   
    var emoticoButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /**
     加载视图
     */
    func setupUI() {
    
        axis = .Horizontal
        
        distribution = .FillEqually
        
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        emoticoButton = addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)
        

    
    }
    
    private func addChildButton(imageName: String, type: FHComposeToolBarButtonType) -> UIButton {
    
        let button = UIButton()
        
        button.tag = type.rawValue
        
        button.setImage(UIImage(named: imageName), forState: .Normal)
        
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        
        button.adjustsImageWhenHighlighted = false
        
        
        button.addTarget(self, action: "sendAction:", forControlEvents: .TouchUpInside)
        
        addArrangedSubview(button)
        
        
        return button
        
    }
    
    
    /**
     按钮点击事件
     
     - parameter btn: <#btn description#>
     */
    @objc private func sendAction(btn: UIButton) {
    
        
        let type = FHComposeToolBarButtonType(rawValue: btn.tag)!
        
        
        composeToolBarCellBack?(buttontype: type)
    
    }
    
    
    
    /**
    *  设置 emoji 键盘的 图片
    */
    
    func showEmotionIcon(isEmoji: Bool) {
    
        
        if isEmoji {
        
            emoticoButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: .Normal)
            
            emoticoButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: .Highlighted)
        
        } else {
        
            emoticoButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: .Normal)
            
            emoticoButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: .Highlighted)
        
        }
        
        
    }
    
    

}
